<template>
    <view>
        <!-- pages/questionnaire/questionnaire.wxml -->
        <view class="containView">
            <view class="recommend">
                <view class="recommendList">
                    <block v-for="(item, index) in datalist" :key="item.tid">
                        <view class="recommendCell" :id="item.tid" hover-class="cellhover" @tap="cellClick">
                            <view class="cell-index">{{ index + 1 }}</view>

                            <view class="cell-content">
                                <view class="cellsubject">
                                    {{ item.subject }}
                                    <text v-if="item.maxchoices != 0">（{{ item.maxchoices > 1 ? '多选' : '单选' }}）</text>
                                </view>
                                <view class="q-info">
                                    <rich-text class="celltime" :nodes="item.dateline"></rich-text>
                                    <view class="author">{{ item.nickname }}</view>
                                </view>
                            </view>
                        </view>
                    </block>
                </view>
            </view>
            <view class="noMoreView" v-if="noMore">别再用力了，你已经滑到底啦</view>
        </view>

        <image :src="minImgDoc + 'course_post.png'" class="postEnter borderShowdow" @tap.stop.prevent="postEnter">发布</image>
    </view>
</template>

<script>
// pages/questionnaire/questionnaire.js
const workListUrl = require('../../config').forumdisplayUrl;

const minImgDoc = require('../../config').minImgDoc;

const app = getApp();

export default {
    data() {
        return {
            minImgDoc: minImgDoc,
            datalist: [],
            loading: false,
            pagenum: 1,
            fid: 36,
            noMore: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {

        this.shareManage(options);
        this.makeRequest();
    },
    onPullDownRefresh: function () {
        uni.showLoading({
            title: 'loading...',
            icon: 'loading'
        });
        this.requestMore(false);
    },
    onReachBottom: function () {
        if (!this.noMore) {
            this.requestMore(true);
        }
    },
    methods: {
        shareManage(options) {
            // sharetype = work & shareid=
            if (options.shareid) {
                uni.navigateTo({
                    url: '../questionnaire_detail/questionnaire_detail?tid=' + options.shareid
                });
            }
        },

        makeRequest() {
            this.loading= true;
            let getdata = {
                fid: this.fid,
                page: this.pagenum // mobile_api:1
            };
            app.globalData.apimanager
                .getRequest(workListUrl, getdata)
                .then((res) => {
                    uni.stopPullDownRefresh();
                    uni.hideLoading();
                    let arr1 = res.Variables.forum_threadlist;

                    for (let i = 0; i < arr1.length; i++) {
                        var thread = arr1[i];

                        if (res.Variables.usernicknames[thread.authorid]) {
                            thread.nickname = res.Variables.usernicknames[thread.authorid];
                        } else {
                            thread.nickname = thread.author;
                        }
                    }

                    if (this.pagenum > 1) {
                        arr1 = this.datalist.concat(arr1);
                    }

                    var noMore = false;

                    if (arr1.length >= res.Variables.forum.threadcount) {
                        noMore = true;
                    }

                    this.setData({
                        loading: false,
                        datalist: arr1,
                        noMore: noMore
                    });
                })
                .catch((res) => {
                    uni.stopPullDownRefresh();
                    uni.hideLoading();
                    this.loading= false;
                    uni.showToast({
                        title: '出错了！',
                        icon: 'none'
                    });
                });
        },

        stopPullDownRefresh() {
            uni.stopPullDownRefresh({
                complete(res) {
                    uni.hideToast();
                }
            });
        },

        requestMore(isMore) {
            let pagenum = this.pagenum;

            if (isMore) {
                pagenum += 1;
            } else {
                pagenum = 1;
            }

            this.pagenum= pagenum // 更新当前页数;
            this.makeRequest(); // 重新调用请求获取下一页数据 或者刷新数据
        },

        cellClick(e) {
            const id = e.currentTarget.id;
            uni.navigateTo({
                url: '../questionnaire_detail/questionnaire_detail?tid=' + id
            });
        },

        postEnter() {
            uni.navigateTo({
                url: '../post_question/post_question?fid=' + this.fid
            });
        }
    }
};
</script>
<style>
@import './questionnaire.css';
</style>
